<!--单图上传组件/按钮-->
<template>
  <div class="upload-container">
    <el-upload
      :action="options.host"
      name="file"
      :multiple="false"
      :show-file-list="false"
      :before-upload="beforeUpload"
      :on-error="handleUploadError"
      :on-success="handleSuccess"
      :drag="drag"
      list-type="picture"
      :data="formData"
    >
      <!-- :data="options.formData" -->
      <img v-if="imageUrl" height="100%" width="100%" :src="imageUrl">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
    </el-upload>
  </div>
</template>

<script>
export default {
  name: 'SingleUpload',
  model: {
    prop: 'imageUrl'
  },
  props: {
    imageUrl: {
      type: String,
      default: ''
    },
    // 是否推拽
    drag: {
      type: Boolean,
      default: false
    },
    // 推拽宽度
    dialogWidth: {
      type: String,
      default: '35%'
    },
    // 接口请求图片地址的type类型
    type: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      url: '',
      formData: {
        'policy': '',
        'authorization': ''
      },
      options: {
        auth: '',
        bucket: '',
        domain: '',
        host: '',
        saveKey: ''
      },
      dialogVisible: false,
      uploadUrl: ''
      // imageUrl: ''

    }
  },
  computed: {
    uploadClass() {
      return this.drag ? 'image-uploader' : ''
    }
  },
  methods: {
    // 上传成功
    handleSuccess(res) {
      console.log(res)
      let imageUrl = this.options.domain + this.options.saveKey
      this.$emit('success', imageUrl)
    },
    // 失败
    handleUploadError(err) {
      this.$message.error(err.message)
      this.$emit('err')
    },
    async beforeUpload(file) {
      const res = await this.$api.menberCard.upLoad({
        'floder': this.type
      })
      this.options = res.data
      this.formData = {
        authorization: res.data.auth,
        policy: res.data.policy
      }
    }
  }
}
</script>

<style scoped>
  .avatar-uploader {
    width: 178px;
    height: 178px;
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

</style>
